package com.jwthhealth_pub.wristband.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.jwthhealth_pub.old.common.Constant;

import java.util.ArrayList;
import java.util.List;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 蓝牙、手环消息广播
 * Created by liuzhen on 2017/12/22.
 */

public class WristBandReceiver extends BroadcastReceiver {

    private static final String TAG = makeLogTag(WristBandReceiver.class);

    public static final int BLUETOOTH_NOTSUPPORT = 0; //不支持蓝牙
    public static final int BLUETOOTH_OFF = 1; //蓝牙关闭
    public static final int NOTIFY_STEP_CHANGED = 2; // 计步广播
    public static final int NOTIFY_HEART_CHANGED = 3; // 心率广播
    public static final int NOTIFY_BLOOD_CHANGED = 4; // 血压广播
    public static final int NOTIFY_OXYGEN_CHANGED = 5; // 血氧广播
    public static final int NOTIFY_FATIFUE_CHANGED = 6; // 疲劳度广播
    public static final int NOTIFY_SLEEP_CHANGED = 7; // 睡眠广播

    private onWristBandDataChanged mOnWristBandDataChanged;

    public void setOnWristBandDataChanged(onWristBandDataChanged mOnWristBandDataChanged) {
        this.mOnWristBandDataChanged = mOnWristBandDataChanged;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action == null) return;

        if (action.equals(Constant.PACKAGENAME)) {
            int tag = intent.getIntExtra(Constant.TAG, -1);
            dispatch(tag, intent);
        }
    }

    private void dispatch(int tag, Intent intent) {
        Log.d(TAG, "tag:" + tag);
        switch (tag) {
            case BLUETOOTH_NOTSUPPORT:
                Log.d(TAG, "bluetooth notsupport");
                break;
            case BLUETOOTH_OFF:
                Log.d(TAG, "bluetooth off");
                break;
            case NOTIFY_STEP_CHANGED:
                if (mOnWristBandDataChanged != null){
                    List<String> params = intent.getStringArrayListExtra(Constant.WRISTBAND_DATA);
                    mOnWristBandDataChanged.onStepChange(params);
                }
                break;
            case NOTIFY_HEART_CHANGED:
                String heartValue = intent.getStringExtra(Constant.WRISTBAND_DATA);
                Log.d(TAG, heartValue);
                mOnWristBandDataChanged.onHeartChange(heartValue);
                break;
            case NOTIFY_BLOOD_CHANGED:
                ArrayList<String> bloodValue = intent.getStringArrayListExtra(Constant.WRISTBAND_DATA);
                mOnWristBandDataChanged.onBloodPressureChange(bloodValue);
                break;
            case NOTIFY_OXYGEN_CHANGED:
                String oxyValue = intent.getStringExtra(Constant.WRISTBAND_DATA);
                mOnWristBandDataChanged.onOxyBloodChange(oxyValue);
                break;
            case NOTIFY_FATIFUE_CHANGED:
                String fatifue = intent.getStringExtra(Constant.WRISTBAND_DATA);
                mOnWristBandDataChanged.onFatifueChange(fatifue);
                break;
            case NOTIFY_SLEEP_CHANGED:
                ArrayList<String> sleepValues = intent.getStringArrayListExtra(Constant.WRISTBAND_DATA);
                Log.d(TAG, "sleepValues:" + sleepValues);
                mOnWristBandDataChanged.onSleepChange(sleepValues);
                break;
            default:
                break;
        }
    }

    public interface bluetoothOff {
        void onBlueToothOff();
    }

    public interface onWristBandDataChanged {
        void onStepChange(List<String> stepParas);

        void onHeartChange(String value);

        void onBloodPressureChange(ArrayList<String> value);

        void onOxyBloodChange(String oxyValue);

        void onFatifueChange(String fatifue);

        void onSleepChange(ArrayList<String> sleepValues);
    }


}
